limsMethod.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-method-edit-list-grid',
            url : limsMethod.baseUrl + '/queries/raw',
            service : 'limsMethodServiceImpl',
            dbTable : 'T_LIMS_METHOD',
            columns : limsMethod.getEditListColumns(),
            page : true,
            deleteFormatter : function(row) {
                return Gikam.propI18N("T_LIMS_METHOD.METHODCODE") +': '+ row.methodCode
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'lims-method-edit-list-base-info-form' ]);
                }
            },
            onRowActive : function(index, row) {
                Gikam.getAsyncComp('method-version-list-grid').done(function(grid) {
                    grid.refresh({
                        url : limsMethod.versionUrl + '/queries/raw',
                        requestData: {
                            methodId_EQ: row.id
                        }
                    })
                })
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    Gikam.preInsert({
                        modalTitle : 'GIKAM.BUTTON.INSERT',
                        fields : limsMethod.getBaseInfoFormFields(),
                        url : limsMethod.baseUrl
                    }).done(function(id) {
                        Gikam.getComp('lims-method-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-method-edit-list-grid').deleteRows(limsMethod.baseUrl);
                }
            } ]
        }
    },

    getVersionsParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'method-version-list-grid',
            columns : limsMethod.getVersionListColumns(),
            service : 'limsMethodVersionServiceImpl',
            deleteFormatter : function(row) {
                return Gikam.propI18N("T_LIMS_METHOD_VERSION.METHODVERSION") +': '+ row.methodVersion || row.id
            },
            onRowActive : function(index, row) {
                Gikam.getComp('method-version-detail-uploader').setOptions({
                    bizId : row.id
                });
            },
            onBeforeUpdate: function (row, keys) {
                var actRow = this.getActivedRow()
                if (keys.includes('startDate') && row.startDate && row.startDate !== '') {
                    if (actRow.expDate && actRow.expDate !== '') {
                        var startDate = new Date(row.startDate)
                        var endDate = new Date(actRow.expDate)
                        if (startDate > endDate) {
                            Gikam.alert(Gikam.propI18N('T_LIMS_METHOD_VERSION.TIP.DATE_VALIDATOR'))
                            this.setData({
                                startDate: '',
                                id: actRow.id,
                                index: actRow.index
                            }, false)
                            this.refreshRowById(actRow.id)
                            return false
                        }
                    }
                }
                if (keys.includes('expDate') && row.expDate && row.expDate !== '') {
                    if (actRow.startDate && actRow.startDate !== '') {
                        var startDate = new Date(actRow.startDate)
                        var endDate = new Date(row.expDate)
                        if (startDate > endDate) {
                            Gikam.alert(Gikam.propI18N('T_LIMS_METHOD_VERSION.TIP.DATE_VALIDATOR'))
                            this.setData({
                                expDate: '',
                                id: actRow.id,
                                index: actRow.index
                            }, false)
                            this.refreshRowById(actRow.id)
                            return false
                        }
                    }
                }
            },
            onLoadSuccess : function(rows) {
                if (!Gikam.isEmpty(rows)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.getComp('method-version-detail-uploader').setOptions({
                        bizId : '-1'
                    });
                }
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add-row',
                onClick : function() {
                    var activedRow = Gikam.getComp('lims-method-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                        return false;
                    }
                    Gikam.getComp('method-version-list-grid').insert(limsMethod.versionUrl, [{
                        methodId: activedRow.id
                    }])
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('method-version-list-grid').deleteRows(limsMethod.versionUrl);
                }
            }]
        }
    },

    getUploader : function() {
        var _this = this;
        return {
            type : 'uploader',
            immediateUpload : true,
            id : 'method-version-detail-uploader',
            filter : false,
            dbTable : 'T_METHOD_VERSION',
            onBeforeUpload : function() {
                var activedRow = Gikam.getComp('method-version-list-grid').getActivedRow();
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                    return false;
                }
            },
        }
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-method-edit-list-layout',
            renderTo : workspace.window.$dom,
            west : {
                width : '40%',
                title : 'T_LIMS_METHOD.SERVICE_NAME',
                items : [ _this.getGridParam() ]
            },
            center : {
                items : [ {
                    type : 'layout',
                    north : {
                        height : '40%',
                        title : 'T_LIMS_METHOD_VERSION.SERVICE_NAME',
                        items : [ _this.getVersionsParam() ]
                    },
                    center : {
                        title : 'GIKAM.FILE.ATTACHMENT',
                        items : [ _this.getUploader() ]
                    }
                } ]
            },
        });
    },

    init : function() {
        this.create();
    }
};
